package bill.layuiboot.service;

import bill.layuiboot.entity.Product;
import bill.layuiboot.entity.ProductCategory;
import bill.layuiboot.mapper.ProductCategoryMapper;
import bill.layuiboot.mapper.ProductMapper;
import bill.layuiboot.vo.DataVO;
import bill.layuiboot.vo.ProductVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Bill Ludwig; 2020/5/31 17:37
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper mapper;

    @Resource
    private ProductCategoryMapper categoryMapper;

    @Override
    public int edit(Integer id, Float price) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Product product = mapper.selectOne(wrapper);
        product.setPrice(price);
        return mapper.updateById(product);
    }

    @Override
    public int delete(Integer id) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return mapper.delete(wrapper);
    }

    /**
     * 模拟json数据，需要创建VO
     * @return 啊
     */
    @Override
    public DataVO<ProductVO> findData(Integer page, Integer limit) {
        DataVO<ProductVO> data = new DataVO<>();
        data.setCode(0);
        data.setMsg("");
//        data.setCount(mapper.selectCount(null));

        // 分页
        IPage<Product> productIPage = new Page<>(page, limit);
        IPage<Product> res = mapper.selectPage(productIPage, null);
        data.setCount(res.getTotal());  // 拿到分页后的总数数据

        List<Product> products = res.getRecords();  // 拿到分完页后的数据
        List<ProductVO> productVOS = new ArrayList<>();
        for (Product product : products) {
            var productVO = new ProductVO();
            BeanUtils.copyProperties(product, productVO);  // 将实体类复制到vo类

            // 根据product的levelid获取到对应的ProCate类数据
            QueryWrapper<ProductCategory> wrapper = new QueryWrapper<>();

            // wrapper中的id是ProCate类的id，后面的是product的levelid
            wrapper.eq("id", product.getCategoryleveloneId());
            ProductCategory productCategory = categoryMapper.selectOne(wrapper);
            if (productCategory != null) productVO.setCategorylevelone(productCategory.getName());

            wrapper = new QueryWrapper<>();
            wrapper.eq("id", product.getCategoryleveltwoId());
            productCategory = categoryMapper.selectOne(wrapper);
            if (productCategory != null) productVO.setCategoryleveltwo(productCategory.getName());

            wrapper = new QueryWrapper<>();
            wrapper.eq("id", product.getCategorylevelthreeId());
            productCategory = categoryMapper.selectOne(wrapper);
            if (productCategory != null) productVO.setCategorylevelthree(productCategory.getName());

            productVOS.add(productVO);
        }

        data.setData(productVOS);

        return data;
    }
}
